03. Writing Unit Tests

Writing Unit Tests with JUnit

ND079 JPND C3 L4 A03 Writing Unit Tests With JUnit V2

Identifying Test Methods

Once you have a test class, you can mark methods in that class as tests by using the @Test annotation.

@Test
public void myUnitTest() {
    //tada! (always passes)
}

Using Assertions

Unit tests always pass unless something causes them to fail. An uncaught exception will cause tests to fail, but JUnit provides the Assertions class to cause conditional test failure. You can use Assertions.assertEquals to rewrite the earlier test method.

@Test
public void addTwoNumbers_fiveAndSix_returnsEleven() {
   int input1 = 5;
   int input2 = 6;
   int expected = 11;
   App app = new App();
   Assertions.assertEquals(expected, app.addTwoNumbers(input1, 
      input2), "5 + 6 should always equal 11");
}

Types of Assertions

  • assertEquals(expected, actual) / assertNotEquals - Fails if the expected an actual values don't match. Available for all primitive data types, as well as Object.
  • assertArrayEquals(expected, actual) - Fails if any element of the two arrays are not equal. Available for all primitive arrays as well as Object[].
  • assertIterableEquals(expected, actual) - Like ArrayEquals, but for Iterable collections
  • assertTrue(condition) / assertFalse - Fails if condition doesn't match what method expects.
  • assertNull(object) / assertNotNull - Fails if object null status does not match what method expects.
  • assertThrows(Class exceptionType, executable) / assertDoesNotThrow - Runs the executable and fails if the provided exception type is not thrown
  • assertAll(Executable... executables) - Runs all the executables and fails if any of the executables fail
  • fail - Always fails

Using assertAll

Each test aborts as soon as any assertion fails, but if you wish to make sure multiple conditions are checked within the same unit test, you can use the assertAll method. This test verifies that all three properties match, and the report on this test will indicate all properties that do not match instead of failing in the middle.

@Test
public void getCat_returnsBeigeSonicAge10(){
   Cat cat = catTest.getCat();
   assertAll("This Message Will Print In The Test Report",
           () -> assertEquals("Sonic", cat.getName()),
           () -> assertEquals(Color.BEIGE, cat.getColor()),
           () -> assertEquals(10, cat.getAge())
   );
}

Naming Unit Tests

ND079 JPND C3 L4 A04 Naming Unit Tests V2

Test Naming Conventions

There are many different test naming conventions. For this course, our recommendations are:

  • Don't use the worst 'test' in the name.
  • Start the test name with the method or scenario under test.
  • Include the condition you're testing.
  • End with the expected result.
  • Separate each name fragment with underscores.

DisplayName

You can override the displayed test name using the @DisplayName annotation. This allows special characters and spaces, so you can write more readable test names.

@Test
@DisplayName("Verify 5+6 = 11 \uD83D\uDE2C")
public void addTwoNumbers_fiveAndSix_returnsEleven() {
    int input1 = 7;
    int input2 = 6;
    int expected = 11;
    App app = new App();
    Assertions.assertEquals(expected, app.addTwoNumbers(input1, input2), "5 + 6 should always equal 11");
}